import {_decorator} from "cc";
import {ResourceUtil} from "../utils/ResourceUtil";

const {ccclass, property} = _decorator;

@ccclass("DataManager")
export class DataManager {
    /**
     * 已经加载过的数据文件，避免重复加载
     */
    private _dictData: any = {}

    /**
     * 读取本地数据
     * @param dataPath 本地数据路径
     * @private
     */
    private readLocalData(dataPath: string, cb: Function) {
        // 是否已经加载过
        if (this._dictData.hasOwnProperty(dataPath)) {
            // 回调
            cb && cb(null, this._dictData[dataPath])
            return;
        }

        // 开始加载数据
        ResourceUtil.getJsonData(dataPath, (err, text) => {
            // 缓存
            this._dictData[dataPath] = text;
            // 回调
            cb && cb(err, text)
        })
    }
}
